package com.berchen.test;

/**
 * 买票
 */
public class MaiPiaoDemo {
    private volatile int num=100;

    public void maipiao() throws InterruptedException {
        if (num>0) {
            synchronized (this) {
                if (num>0) {
                    System.out.println(Thread.currentThread().getName() + "买票：" + num--);
                }
            }
        }
    }

    public static void main(String[] args) {
        MaiPiaoDemo maiPiaoDemo = new MaiPiaoDemo();
        for (int i = 0; i < 100; i++) {

            new Thread(()->{
                try {
                    maiPiaoDemo.maipiao();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }).start();
        }
    }
}
